package au.edu.unsw.editingtool.db;

import java.sql.SQLException;
import java.util.Hashtable;

import au.edu.unsw.editingtool.global.EditingToolException;

public class DBUser extends DBManager {
	
	private void setupDBMapping() {
		if (MappingTable.size() > 0)
			return;
		MappingTable.put("UserID", "uid");
		MappingTable.put("LastName", "lastname");
		MappingTable.put("OtherNames", "othername");
		MappingTable.put("Title", "title");
		MappingTable.put("PreferedLastName", "prefer_lastname");
		MappingTable.put("PreferedOtherNames", "prefer_othername");
		MappingTable.put("PrimaryEmail", "primary_email");
		MappingTable.put("ScondaryEmail", "secondary_email");
	}
	
	public DBUser() {
		this.table = "userinfo";
		this.primaryKeys = new String[]{"UserID"}; 
		this.setupDBMapping();
	}
	
	public String addUser(String lastname, String othername, String title, 
		String pLastname, String pOthername, String pEmail, String sEmail) 
		throws EditingToolException, ClassNotFoundException, SQLException 
	{
		this.setTable(this.table, "");
		Hashtable<String,String> userInfo = new Hashtable<String,String>();
		userInfo.put("LastName",lastname);
		userInfo.put("OtherNames",othername);
		userInfo.put("Title",title);
		userInfo.put("PreferedLastName",pLastname);
		userInfo.put("PreferedOtherNames",pOthername);
		userInfo.put("PrimaryEmail",pEmail);
		userInfo.put("ScondaryEmail",sEmail);		
		this.setColumnValues(userInfo);		
		this.initDB();
		this.insert(false, true);
		String uid = this.getGenerateKey().toString();
		this.clean();
		return uid;
	}
	
	// Return the uid if exists and return 0 if there is no user
	public String userExists(String lastname, String othername, String title, 
			String pLastname, String pOthername, String pEmail, String sEmail) 
			throws EditingToolException, ClassNotFoundException, SQLException 
		{
			String result = null;
			this.setTable(this.table, "");
			String fields[] = {"UserID"};				
			this.setFieldsCondition("LastName", this.setEqual(lastname));
			this.setFieldsCondition("OtherNames", this.setEqual(othername));
			this.setFieldsCondition("Title", this.setEqual(title));
			this.setFieldsCondition("PreferedLastName", this.setEqual(pLastname));
			this.setFieldsCondition("PreferedOtherNames", this.setEqual(pOthername));
			this.setFieldsCondition("PrimaryEmail", this.setEqual(pEmail));
			this.setFieldsCondition("ScondaryEmail", this.setEqual(sEmail));							
			this.addField(fields);
			this.initDB();
			this.selectRecords();
			
			if (this.ResultSet == null) {
				this.clean();
				return "0";
			}
			if(this.ResultSet.getFetchSize()==1)
			{			
				while(ResultSet.next())
				{
					result = ResultSet.getObject(1).toString();
				}
				this.clean();
				return result;	
			}
			else
			{
				this.clean();
				return "0";		
			}
		}
	
	public void deleteUser(Hashtable<String, String> userInfo) throws EditingToolException, ClassNotFoundException, SQLException 
	{
	
	}
	
}
